/*
 * Copyright (C) 2016 yixun All Rights Reserved.
 * 
 * SignedServiceImpl.java
 */
package com.emm.yixun.basic.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.emm.yixun.basic.mapper.SignedMapper;
import com.emm.yixun.basic.service.SignedService;
import com.emm.yixun.common.model.Signed;
import com.emm.yixun.common.model.SignedInfo;
import com.emm.yixun.common.model.dto.SignedDto;
import com.emm.yixun.common.page.Page;

/**
 * [文件名称]<br>
 * SignedServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 客户签约表 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2016-05-11 11:27:12 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("signedService")
public class SignedServiceImpl implements SignedService {

	@Autowired
	private SignedMapper mapper;
	

	/**
	 * @param entity
	 *            搜索条件
	 * @param begin
	 *            分页开始行数[不包含]
	 * @param end
	 *            分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<Signed> getPageModel(SignedDto entity, int begin, int end) {
		Integer total = mapper.count(entity);
		List<Signed> rows = mapper.list(entity, begin, end);
		Page<Signed> msr = new Page<Signed>(total, rows);
		return msr;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.emm.yixun.basic.service.SignedService#getPageDtoModel(com.emm.yixun.common.model.dto.SignedDto, int,
	 * int)
	 */
	public Page<SignedDto> getPageDtoModel(SignedDto entity, int begin, int end) {
		Integer total = mapper.countDto(entity);
		if(!StringUtils.isEmpty(entity.getIsExport())){
			begin=0;
			end = total;
			entity.setOrders(null); //导出不按列表查询方式排序
		}
		List<SignedDto> rows = mapper.listDto(entity, begin, end);
		Page<SignedDto> msr = new Page<SignedDto>(total, rows);
		msr.setPageSize(end);
		return msr;
	}

	/**
	 * 查询列表
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public List<Signed> selectListByDto(SignedDto entity) {
		return mapper.selectListByDto(entity);
	}

	/**
	 * 查询单个
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public Signed selectByDto(SignedDto entity) {
		return mapper.selectByDto(entity);
	}

	/**
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public Signed selectById(Long id) {
		if (id != null) {
			return mapper.selectById(id);
		}
		return null;
	}

	/**
	 * @param entity
	 *            新增记录的详细信息
	 * @return 新增行数
	 */
	public SignedDto create(SignedDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity
	 *            更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(SignedDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}

	/**
	 * @param entity
	 *            待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(SignedDto entity) {
		return mapper.del(entity);
	}

	/**
	 * @param entity
	 *            新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(SignedDto entity) {
		if (entity != null && entity.getId() != null) {
			return update(entity);
		} else {
			create(entity);
			return 1;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.emm.yixun.basic.service.SignedService#selectPageDtoModel(com.emm.yixun.common.model.dto.SignedDto, int,
	 * int)
	 */
	public Page<SignedDto> selectPageDtoModel(SignedDto entity, int begin, int end) {
		Integer total = mapper.selectCountDto(entity);
		List<SignedDto> rows = mapper.selectListDto(entity, begin, end);
		Page<SignedDto> msr = new Page<SignedDto>(total, rows);
		return msr;
	}
	
	public List<SignedDto> selectAllDto(SignedDto entity) {
		return mapper.selectAllDto(entity);
	}

	public SignedDto selectChangeSignedByDto(SignedDto entity) {
		return mapper.selectChangeSignedByDto(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.emm.yixun.basic.service.SignedService#selectSignedCustomer(java.lang.Long)
	 */
	public SignedDto selectSignedCustomer(Long id) {
		return mapper.selectSignedCustomer(id);
	}

	public SignedInfo totalNumberAndAmount(SignedDto entity) {
		return mapper.totalNumberAndAmount(entity);
	}
	
	/*
	 * 客户详情页查询签约列表
	 */
	public Page<SignedDto> getPageDtoByCustomerId(String customerId, int begin, int end) {
		Integer total = mapper.getCountByCustomerId(customerId);
		List<SignedDto> rows = mapper.getListByCustomerId(customerId, begin, end);
		Page<SignedDto> msr = new Page<SignedDto>(total, rows);
		msr.setPageSize(end);
		return msr;
	}

	public Date findMaxSignedCheckDate(Long projectId, String industMark) {
		return mapper.findMaxSignedCheckDate(projectId, industMark);
	}
	
	/**
	 * 根据房间ID查询客户姓名和经纪人
	 * @param id
	 * @return
	 */
	public SignedDto getCustomerByRoomId(Long roomId) {
		return mapper.getCustomerByRoomId(roomId);
	}

	public Page<SignedDto> getSignedListPageModel(SignedDto entity, int begin, int end) {
		Integer total = mapper.getSignedCountPageModel(entity);
		List<SignedDto> rows = mapper.getSignedListPageModel(entity,begin,end);
		Page<SignedDto> cpc = new Page<SignedDto>(total, rows);
		cpc.setPageSize(end);
		return cpc;
	}

	public List<SignedDto> getSignedListByDto(SignedDto entity) {
		List<SignedDto> rows = mapper.getSignedListByDto(entity);
		return rows;
	}
}
